package oldrouter.gui;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JCheckBox;
import java.awt.Insets;

import router.base.router.RouterException;

import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

import oldrouter.Router;
import oldrouter.usb.UsbClient;
import oldrouter.usb.UsbException;
import oldrouter.usb.UsbPort;

import logger.gui.FilterLogPanel;

public class UsbPanel extends JPanel implements RouterDependant{
	private static final long serialVersionUID = 5486549625775131883L;
	private FilterLogPanel logPanel;
	private UsbClient usbClient;
	private JTextField txtCom;
	private JButton btnStart, btnStop;
	private int routerPort;
	private JTextField textField;
	private JButton btnEnvoyer;
	private JLabel lblCodeHexadcimal;
	
	public UsbPanel() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		txtCom = new JTextField();
		txtCom.setText("COM1");
		GridBagConstraints gbc_txtCom = new GridBagConstraints();
		gbc_txtCom.insets = new Insets(0, 0, 5, 5);
		gbc_txtCom.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtCom.gridx = 0;
		gbc_txtCom.gridy = 0;
		add(txtCom, gbc_txtCom);
		txtCom.setColumns(10);
		
		GridBagConstraints gbc_btnStart = new GridBagConstraints();
		gbc_btnStart.insets = new Insets(0, 0, 5, 0);
		gbc_btnStart.gridx = 2;
		gbc_btnStart.gridy = 0;
		
		btnStart = new JButton("Initialiser Port USB");
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					UsbPort usbPort = new UsbPort(txtCom.getText(), logPanel);
					usbClient = new UsbClient(usbPort, "USB", "localhost", routerPort, logPanel);
					usbPort.setMsgListener(usbClient);
					btnStart.setVisible(false);
					btnStop.setVisible(true);
				} catch (UsbException e) {
					logPanel.err(e.toString());
				} catch (RouterException e) {
					logPanel.err(e.toString());
				}
			}
		});
		btnStart.setEnabled(false);
		add(btnStart, gbc_btnStart);
		
		GridBagConstraints gbc_btnStop = new GridBagConstraints();
		gbc_btnStop.insets = new Insets(0, 0, 5, 0);
		gbc_btnStop.gridx = 2;
		gbc_btnStop.gridy = 0;
		btnStop = new JButton("Fermer Port USB");
		btnStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				usbClient.close();
				btnStart.setVisible(true);
				btnStop.setVisible(false);
			}
		});
		btnStop.setVisible(false);
		add(btnStop, gbc_btnStop);
		
		JCheckBox chckbxActiverLenvoiDes = new JCheckBox("Activer l'envoi des messages");
		chckbxActiverLenvoiDes.setEnabled(false);
		GridBagConstraints gbc_chckbxActiverLenvoiDes = new GridBagConstraints();
		gbc_chckbxActiverLenvoiDes.gridwidth = 3;
		gbc_chckbxActiverLenvoiDes.insets = new Insets(0, 0, 5, 0);
		gbc_chckbxActiverLenvoiDes.gridx = 0;
		gbc_chckbxActiverLenvoiDes.gridy = 1;
		add(chckbxActiverLenvoiDes, gbc_chckbxActiverLenvoiDes);
		
		lblCodeHexadcimal = new JLabel("Code hexad\u00E9cimal");
		GridBagConstraints gbc_lblCodeHexadcimal = new GridBagConstraints();
		gbc_lblCodeHexadcimal.insets = new Insets(0, 0, 5, 5);
		gbc_lblCodeHexadcimal.anchor = GridBagConstraints.EAST;
		gbc_lblCodeHexadcimal.gridx = 0;
		gbc_lblCodeHexadcimal.gridy = 2;
		add(lblCodeHexadcimal, gbc_lblCodeHexadcimal);
		
		textField = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 2;
		add(textField, gbc_textField);
		textField.setColumns(10);
		
		btnEnvoyer = new JButton("Envoyer");
		btnEnvoyer.setEnabled(false);
		GridBagConstraints gbc_btnEnvoyer = new GridBagConstraints();
		gbc_btnEnvoyer.insets = new Insets(0, 0, 5, 0);
		gbc_btnEnvoyer.gridx = 2;
		gbc_btnEnvoyer.gridy = 2;
		add(btnEnvoyer, gbc_btnEnvoyer);
		
		logPanel = new FilterLogPanel();
		GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
		gbc_tabbedPane.gridwidth = 3;
		gbc_tabbedPane.fill = GridBagConstraints.BOTH;
		gbc_tabbedPane.gridx = 0;
		gbc_tabbedPane.gridy = 3;
		add(logPanel, gbc_tabbedPane);
		
	}

	@Override
	public void routerStarted(Router router) {
		if(router!=null) {
			btnStart.setEnabled(true);
			routerPort = router.port;
		} else {
			btnStart.setEnabled(false);
		}
		
	}

	public void dispose() {
		usbClient.close();
	}
	
}
